/*checkLogin();*/
var xcx_openid = GetQueryString('xcx_openid') || '';
var wx_openid = GetQueryString('wx_openid') || '';
var buyer_openid = GetQueryString('buyer_openid') || '';
var sid = GetQueryString('sid') || '';
if (xcx_openid || wx_openid) {
    var success = function (res) {
        var info = res.data.info;
        if (info != '' && info != undefined) {
            setCache(login_key, info);
            init_pay();
            //小程序强制退出授权
            if (window.__wxjs_environment == 'miniprogram') {
                var unionid = info.unionid || '';
                if (window.location.host == 'www.qudingshui.com' && unionid == '') {
                    if ($.inArray(sid, [3, 4, 8])) {
                        setTimeout(function(){ wx.miniProgram.redirectTo({url: '/pages/index/index?type=clear'});},2000)
                    }
                }
            }
        }
    }
    getData(member_info, {xcx_openid: xcx_openid, wx_openid: wx_openid, sid: sid}, success);
}

function init_pay() {
    getSliders();

    mui.init({
        swipeBack: false,
    });

    var login_value = getCache(login_key);
    var sid = login_value.sid;
    var mid = login_value.mid;

    var url = order_pay; //表示先默认为微信 即线上订水

//定义默认支付方式
    var pay_type = 0;//默认微信支付

    var confirm_content = '首次订桶装水，无水桶需付桶押金，一次性桶装水无需押金，送水工线下收，可退';
    var con_type = 1;
    var alert_status = false;

//侧滑容器父节点
    var offCanvasWrapper = mui('#offCanvasWrapper');
    var goods_price_current = 0;

//当前产品的水票数量
    var current_ticket_num = 0;

    var current_buy_num = 1;//校验是否超出配送限度

// 轮播图
    function getSliders() {
        goods_price_current = 0;
        current_ticket_num = 0;
        var success = function (res) {
            console.log(res)

            var max_num = 0;//水票的最大购买量
            var product_max_num = 0;//桶装水的最大购买量

            url = order_pay;
            pay_type = 0
            var slider = res;
            var goods_list = slider.data;
            if(goods_list !=undefined && goods_list != null && goods_list!='' && goods_list.length > 0){
                goods_list[0].goods_num = 100000000;
                console.log(goods_list)
                $('.has_ticket').removeClass('none');
                $('.no_ticket').addClass('none');
                $('.change_bac').css('background','#fff');

                template.defaults.imports.slider_nums = goods_list.length - 1;
                var html = template('slider_img',slider);
                $('#slider_img_content').append(html);
                mui('#offCanvasContentScroll').scroll();
                var gallery = mui('.mui-slider');
                gallery.slider({
                    interval:0//自动轮播周期，若为0则不自动播放，默认为0；
                });

                //最大可增加数 默认为轮播图的第一个对应参数
                if(isEmpty(goods_list[0].bill_info) || goods_list[0].bill_info.length <=0){
                    max_num = 0;

                }else {
                    max_num = goods_list[0].bill_info.rest_num
                    $('#id').val(goods_list[0].bill_info.id);
                }
                $('#current_rest_num').val(max_num);
                product_max_num = goods_list[0].goods_num;//先默认一个
                goods_price_current = goods_list[0].goods_price;
                $('#tickt_id').val(goods_list[0].id);
                var current_num_buy = $('#mui-numbox-input').val();
                $('#num').val(current_num_buy);
                /*$('#button').attr('type','submit');*/
                //判断如果当前余票数量大于0则默认选中余票购买
                if(max_num >0){
                    url = order_add_bill_order;
                    ajaxOption.url= order_add_bill_order;
                    pay_type = 2;//余票购水
                    $('#type').val(0)
                    $('#item'+pay_type).prop('checked','checked');
                    $('.wx_pay').hide();
                    $('.wt_pay').show();
                    $('.totalPrice').hide();
                    //判断是否余票是否为0
                    if(goods_list[0].bill_info.length > 0){
                        var rest_num = goods_list[0].bill_info.rest_num;
                        if(rest_num > 0){
                            $('.toBuy').show();
                        }
                    }
                }else {
                    //微信支付
                    ajaxOption.url = order_pay;
                    // ajaxOption.url = order_add;
                    url = order_pay;
                    pay_type = 1;
                    $('#type').val(0)
                    $('#item'+pay_type).prop('checked','checked')
                    $('.wx_pay').show();
                    $('.wt_pay').hide();
                    $('.totalPrice').show();
                    /*$('#button').attr('type','button');*/
                }

                //根据当前付款方式 判断 给出什么提示
                if(pay_type == 1){//微信支付  判断是否超出购物范围
                    is_buy_num();
                }else if(pay_type == 2){//水票支付  判断是否余票为0
                    is_rest_num();
                }

                /*goods_price_current*/
                //计算总共需要多少钱
                var totalPrice = 1 * goods_price_current;
                $('#totalPrice').html(totalPrice);
                $('#order_amount').val(totalPrice);
                $('#goods_nums').val(current_num_buy);
                $('#goods_ids').val(goods_list[0].goods_id);

                //监听轮播图的切换
                document.querySelector('.mui-slider').addEventListener('slide', function(event) {
                    ajaxOption.alert_status = false;
                    ajaxOption.confirm = false;
                    /*setCache('confirm',false);*/
                    $('.toBuy').hide();
                    //将当前第几张图片 信息 放入hidden 中
                    var slideNumbers = event.detail.slideNumber;
                    goods_list[slideNumbers].goods_num = 100000000;
                    $('#slideNumbers').val(slideNumbers);
                    //根据具体业务，动态获得 对应数据
                    /* max_num = goods_list[event.detail.slideNumber].rest_num;*/
                    if(isEmpty(goods_list[event.detail.slideNumber].bill_info) || goods_list[event.detail.slideNumber].bill_info.length <=0){
                        max_num = 0;
                    }else {
                        max_num = goods_list[event.detail.slideNumber].bill_info.rest_num
                        $('#id').val(goods_list[event.detail.slideNumber].bill_info.id);
                    }
                    $('#current_rest_num').val(max_num);
                    current_ticket_num = max_num;
                    product_max_num = goods_list[event.detail.slideNumber].goods_num;
                    goods_price_current = goods_list[event.detail.slideNumber].goods_price;
                    document.getElementById('max-button').removeAttribute("disabled");
                    $('#tickt_id').val(goods_list[event.detail.slideNumber].id);
                    var current_num_buy = $('#mui-numbox-input').val();
                    $('#num').val(current_num_buy);
                    /*$('#button').attr('type','submit');*/
                    //判断如果当前余票数量大于0则默认选中余票购买
                    if(max_num >0){
                        url = order_add_bill_order;
                        pay_type = 2;//余票购水
                        $('#type').val(0)
                        ajaxOption.url= order_add_bill_order;
                        $('#item'+pay_type).prop('checked','checked');
                        $('.wx_pay').hide();
                        $('.wt_pay').show();
                        $('.totalPrice').hide();
                        //判断是否余票是否为0
                        if(goods_list[event.detail.slideNumber].bill_info.length > 0){
                            var rest_num = goods_list[event.detail.slideNumber].bill_info.rest_num;
                            if(rest_num > 0){
                                $('.toBuy').show();
                            }
                        }
                    }else {
                        url = order_pay;
                        ajaxOption.url= order_pay;
                        // ajaxOption.url = order_add;
                        // url = order_add;
                        pay_type = 1;//微信支付
                        $('#type').val(0)
                        $('#item'+pay_type).prop('checked','checked')
                        $('.wx_pay').show();
                        $('.wt_pay').hide();
                        $('.totalPrice').show();
                        /*$('#button').attr('type','button');*/
                    }

                    //根据当前付款方式 判断 给出什么提示
                    if(pay_type == 1){//微信支付  判断是否超出购物范围
                        is_buy_num();
                    }else if(pay_type == 2){//水票支付  判断是否余票为0
                        is_rest_num();
                    }

                    //监听轮播切换时 input中的数字是否超过当前余票多大值
                    var numM = max_num;
                    if(pay_type != 2){
                        numM = product_max_num;
                    }
                    var num = $('#mui-numbox-input').val();
                    var current_num = num;
                    if(num >= numM){
                        if(numM <= 0){
                            $('#mui-numbox-input').val(1);
                            current_num = 1;
                        }else {
                            $('#mui-numbox-input').val(numM);
                            current_num = numM;
                        }
                        document.getElementById('max-button').setAttribute("disabled","disabled");
                        document.getElementById('min-button').removeAttribute("disabled");
                    }
                    /*goods_price_current*/
                    //计算总共需要多少钱
                    var totalPrice = current_num * goods_price_current;
                    $('#totalPrice').html(totalPrice);
                    $('#order_amount').val(totalPrice);
                    $('#goods_nums').val(current_num_buy);
                    $('#goods_ids').val(goods_list[event.detail.slideNumber].goods_id);

                });

                //绑定加 事件
                $('#max-button').on('tap',function () {
                    console.log(max_num)
                    var numM = max_num;
                    //获取 输入框的数据
                    var num = $('#mui-numbox-input').val();
                    var current_num = num;
                    //判断支付方式 如果是水票支付 则 最大值为水票值，如果为其他支付，则最大值为桶的数量
                    if(pay_type != 2){
                        numM = product_max_num;
                    }
                    if(num >= numM){
                        document.getElementById('max-button').setAttribute("disabled","disabled");
                        document.getElementById('min-button').removeAttribute("disabled");
                        if(numM <= 0){
                            $('#mui-numbox-input').val(1);
                            current_num = 1;
                        }else {
                            $('#mui-numbox-input').val(numM);
                            current_num = numM;

                        }
                    }
                    /*goods_price_current*/
                    //计算总共需要多少钱
                    var totalPrice = current_num * goods_price_current;
                    $('#totalPrice').html(totalPrice);
                    $('#order_amount').val(totalPrice);
                    $('#num').val(current_num);
                    $('#goods_nums').val(current_num);

                    /* checkBuyNum(current_num);*/
                    current_buy_num = current_num;
                    /*alert_status = false;
                    setCache('confirm',false);*/
                    ajaxOption.alert = false;
                    ajaxOption.confirm = false;
                    //根据当前付款方式 判断 给出什么提示
                    if(pay_type == 1){//微信支付  判断是否超出购物范围
                        is_buy_num();
                    }else if(pay_type == 2){//水票支付  判断是否余票为0
                        is_rest_num();
                    }
                })

            }else {
                $('.has_ticket').addClass('none');
                $('.no_ticket').removeClass('none');
                $('.change_bac').css('background','#f1f1f1');
            }

            //无水票时 点击跳转到水票列表
            $('.toTickList').on('tap',function () {
                redirect("water-ticket.html");
            })

            $("#mui-numbox-input").bind('input porpertychange',function(){
                var num = $('#mui-numbox-input').val();
                if(num >= max_num ){
                    if( pay_type == 2){
                        if(max_num <= 0){
                            $('#goods_nums').val(1);
                            $('#mui-numbox-input').val(1);
                        }else {
                            $('#mui-numbox-input').val(max_num);
                            $('#goods_nums').val(max_num);
                        }
                        document.getElementById('max-button').setAttribute("disabled","disabled");
                        document.getElementById('min-button').removeAttribute("disabled");
                    }
                }else {
                    $('#goods_nums').val(num);
                    if(num <= 1){
                        document.getElementById('min-button').setAttribute("disabled","disabled");
                    }else {
                        document.getElementById('min-button').removeAttribute("disabled");
                    }
                    if(pay_type == 1){
                        //计算合计支付
                        var totalP = num *goods_price_current;
                        $('#totalPrice').html(totalP);
                    }
                }
                is_rest_num();
            });


            $('.left_img').on('tap',function () {
                var slideNumbers = $('#slideNumbers').val();
                if(slideNumbers <= 0){
                    return;
                }
                //获得slider插件对象
                var gallery = mui('.mui-slider');
                gallery.slider().gotoItem(parseInt(slideNumbers)-1);//跳转到第index张图片，index从0开始；
            })

            $('.right_img').on('tap',function () {
                var slideNumbers = $('#slideNumbers').val();
                if(slideNumbers >= slider.data.length-1){
                    return;
                }
                //获得slider插件对象
                var gallery = mui('.mui-slider');
                gallery.slider().gotoItem(parseInt(slideNumbers)+1);//跳转到第index张图片，index从0开始；
            })

            //若水票用完  点击购买
            $('.toBuy').on('tap',function () {
                var id = this.getAttribute('data-id');
                redirect('water-buy-ticket.html?id='+id);
            })

        };
        getData(goods_bill,comParams(),success);
    }

    /*ajaxOption = new Object();*/
    /*function checkFrim() {*/
    var ajaxOption = {
            url: url,
            method: 'POST',
            alert: alert_status,
            alert_type:2,
            confirm: getCache('confirm'),
            confirm_title: '提示',
            confirm_content: confirm_content,//用[]包起来表示该值是函数，不包则为字符串
            data: combineParams({formId: 1,buyer_openid:buyer_openid,share:1}),// share表示此订单是通过主账号购买
            success: function (res) {
                /*if (ajaxOption.url == order_add) {
                    /!*locationReplace('success.html?type=1');*!/
                    locationReplace('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU1NDc2MzcyNg==#wechat_redirect')
                } */
                if(ajaxOption.url == order_pay){
                    payment(res.data);
                } else {
                    locationReplace('success.html?type=4');
                    /*locationReplace('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU1NDc2MzcyNg==#wechat_redirect')*/
                }
            },
            error: function (res) {
               locationReplace('fail.html?type=3');
            }
        };

        checkform($('#orderForm'), ajaxOption);


        //选择支付方式
        $('.choosePayType').on('tap',function () {
            $('.toBuy').hide();
            var paytype = parseInt(this.getAttribute('paytype'));
            pay_type = paytype;
            // if(pay_type == 2 || pay_type == 0){
            //     $('#type').val(0)
            // }else {
            //     $('#type').val(0)
            // }
            $('#type').val(0)
            $('#item'+pay_type).prop('checked','checked')
            //表示微信支付
            $('.totalPrice').show();
            /*$('#button').attr('type','submit');*/
            if(paytype == 0){
                url = order_pay;
                ajaxOption.url = order_pay;
                $('.wx_pay').show();
                $('.wt_pay').hide();
            }else if(paytype == 1){ //表示微信支付
                url = order_pay;
                ajaxOption.url = order_pay;
                $('.wx_pay').show();
                $('.wt_pay').hide();
                is_buy_num();
            }else if(paytype == 2){//表示水票购买
                url = order_add_bill_order;
                ajaxOption.url = order_add_bill_order;
                $('.wx_pay').hide();
                $('.wt_pay').show();
                $('.totalPrice').hide();
                /*$('#button').attr('type','button');*/
                //判断是否余票是否为0

                if(current_ticket_num <= 0){
                    $('.toBuy').show();
                }
            }
            /*alert_status = false;
            setCache('confirm',false);*/
            ajaxOption.alert =false;
            ajaxOption.confirm  = false;
            //根据当前付款方式 判断 给出什么提示
            if(pay_type == 1){//货到付款  判断是否超出购物范围
                is_buy_num();
            }else if(pay_type == 2){//水票支付  判断是否余票为0
                is_rest_num();
            }
        })
  /*  }*/


// -----------------------------------非表单验证支付--------------------------------------------start
    /*function _ajax() {
        $.ajax({
            type: 'POST',
            url: url,
            data: combineParams({
                formId: 1,
                buyer_name:'ggyy',
                mobile: '15032651231',
                buyer_address: '湖北省 武汉市 洪山区 华工科技园B座1608啊',
                type: 0,
                'goods_ids[]': 28,
                'goods_nums[]': 3,
                description:'',
                order_amount: 66.00,
            }),
            async: true,
            success: function (res) {
                var ajaxres = res;
                if (typeof(res) != 'object') {
                    ajaxres = JSON.parse(res);
                }
                if (ajaxres.status == 1) {
                    if (ajaxres.msg != '' && ajaxres.msg != undefined) {
                        $('.cxvalidation_tip').css('background-color', 'rgb(64,196,2)');
                        $('.cxvalidation_tip').html(ajaxres.msg).addClass('show');
                    }else {
                        payment(res.data);
                    }

                } else {
                    $("button[type='button']").addClass('mui-disabled').attr('disabled');
                    $("input[type='button']").addClass('mui-disabled').attr('disabled');
                    var msg = '';
                    if (ajaxres.msg != '' && ajaxres.msg != undefined) {
                        msg = ajaxres.msg;
                    } else {
                        msg = '未知错误';
                    }
                    $('.cxvalidation_tip').html(msg).addClass('show');
                    setTimeout(function () {
                        $('.cxvalidation_tip').removeClass('show');
                        $('.cxvalidation_tip').css('background-color', 'orangered');
                        //请求成功开启提交
                        $("button[type='button']").removeClass('mui-disabled').removeAttr('disabled');
                        $("input[type='button']").removeClass('mui-disabled').removeAttr('disabled');
                    }, 2000);

                }

            },
            error: function () {
                $('.cxvalidation_tip').html('请求失败').addClass('show');
                var closeTipWait = setTimeout(function () {
                    $('.cxvalidation_tip').removeClass('show');
                    //请求成功开启提交
                    $("button[type='button']").removeClass('mui-disabled').removeAttr('disabled');
                    $("input[type='button']").removeClass('mui-disabled').removeAttr('disabled');
                }, 3000);
            }
        });
    }*/
// -------------------------------------------------------------------------------end




//请求默认地址 以及 地址列表
    var success = function (res) {

        var info = res.data;
        var html = template('address',info);
        //默认地址
        var address = "";
        var addr = "";
        if(info.address_list != undefined && info.address_list != null && info.address_list != '' && info.address_list.length > 0){
            $('.show_noaddress').removeClass('none');
            for(var i=0;i<info.address_list.length;i++){
                if(info.address_list[i].is_default != undefined
                    &&info.address_list[i].is_default != null
                    && info.address_list[i].is_default !=''
                    && info.address_list[i].is_default == 1)
                {
                    address = info.address_list[i];
                    if(address == undefined || address == null || address == ''){
                        html = '<div class="to_addAddress" style="height: 1.5rem;line-height: 1.5rem;"><img style="width: 1.5rem;height: 1.5rem;float: left" src="image/add.png"><span style="color: rgba(0,0,0,.2);font-size: 0.9rem;margin-left: 0.5rem">暂无地址，请点击添加地址</span><img style="width: 1rem;height: 1rem;float: right;margin-top: 0.3rem" src="image/right.png"></div>'
                    }else {
                        addr = address.area_info+" "+address.address;
                    }
                    continue;
                }
            }
        }else {
            $('.show_noaddress').css('display','block');
            html = '<div class="to_addAddress" style="height: 1.5rem;line-height: 1.5rem;"><img style="width: 1.5rem;height: 1.5rem;float: left" src="image/add.png"><span style="color: rgba(0,0,0,.7);font-size: 0.9rem;margin-left: 0.5rem">暂无地址，请点击添加地址</span><img style="width: 1rem;height: 1rem;float: right;margin-top: 0.3rem" src="image/right.png"></div>'
        }

        $('#address_content').append(html);
        $('#address_detail').text(addr);
        $('.no_address').bind('click',function () {
            redirect("add-address.html?index=1");
        })

        //地址列表
        var html = template('address_list',info);
        $('#address_list_content').append(html);
        $('.choosed').addClass('none');
        if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
            for(var i=0;i<info.address_list.length;i++){
                if(info.address_list[i].is_default != undefined
                    &&info.address_list[i].is_default != null
                    && info.address_list[i].is_default !=''
                    && info.address_list[i].is_default == 1)
                {
                    $('.choosed'+info.address_list[i].id).removeClass('none');
                }
            }
        }

        //监听如果遮罩层打开，就关闭
        if(mui('.mui-off-canvas-wrap').offCanvas().isShown())
        {
            mui('.mui-off-canvas-wrap').offCanvas().close();
        }

        //监听遮罩层的点击事件
        mui('.list_ocntent').on('tap', '.ad_list', function () {
            var sPrents = $(this).parents(".list_ocntent");
            var address_id = sPrents.attr('id');
            console.log(address_id)
            if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
                for(var i=0;i<info.address_list.length;i++) {
                    address = info.address_list[i];
                    //判断 如果是已被选中的，则返回
                    if(address.id == address_id && address.is_default == 1){
                        $('.choosed'+address_id).removeClass('none');
                        return;
                    }
                    info.address_list[i].is_default = 0;
                    $('.choosed').addClass('none');
                    if(info.address_list[i].id == address_id){
                        $('.choosed'+address_id).removeClass('none');
                        info.address_list[i].is_default = 1;
                        var address_detail = info.address_list[i];
                        $('.about-name-new').text(address_detail.name);
                        $('.about_mobile').text(address_detail.mobile);
                        var addr = address_detail.area_info+" "+address_detail.address;
                        $('#address_detail').text(addr);
                        //设置默认地址
                        postData(address_set_default,combineParams
                        ({id:address.id,name:address.name,mobile:address.mobile,address:address.address,area_info:address.area_info}));
                        break;
                    }
                }
            }
        });

        //显示或隐藏遮罩层
        $('.nctouch-cart-add-default').on('tap',function () {
            if(!mui('.mui-off-canvas-wrap').offCanvas().isShown())
            {
                $('.mui-off-canvas-left').css('z-index',2);
                mui('.mui-off-canvas-wrap').offCanvas().show();
            }else {
                mui('.mui-off-canvas-wrap').offCanvas().close();
            }
        })

        $('.mui-off-canvas-right').on('tap',function () {
            mui('.mui-off-canvas-wrap').offCanvas().close();
            /*$('.zhezhao').addClass('none');*/
        })

        // 禁止冒泡，禁止跟手势滑动：
        document.getElementsByClassName('mui-inner-wrap')[0].addEventListener('drag', function(event) {
            event.stopPropagation();
        });


        //绑定减去事件
        $('#min-button').on('tap',function () {
            var num = $('#mui-numbox-input').val();
            var current_num = num;
            if(num == 1){
                current_num = 1;
                $('#mui-numbox-input').text(1);
            }

            //计算总共需要多少钱
            var totalPrice = current_num * goods_price_current;
            $('#totalPrice').html(totalPrice);
            $('#order_amount').val(totalPrice);
            $('#goods_nums').val(current_num);
            /*checkBuyNum(current_num);*/
            current_buy_num = current_num;

            /*setCache('confirm',false);
            alert_status = false;*/
            ajaxOption.alert = false;
            ajaxOption.confirm = false;
            //根据当前付款方式 判断 给出什么提示
            if(pay_type == 1){//货到付款  判断是否超出购物范围
                is_buy_num();
            }else if(pay_type == 2){//水票支付  判断是否余票为0
                is_rest_num();
            }
        })

        //无水票时 点击跳转到水票列表
        $('.toTickList').on('tap',function () {
            redirect("water-ticket.html");
        })

        //点击添加地址
        $('.to_addAddress').on('tap',function () {
            redirect("add-address.html?ref=3");

        })


        //获取原始窗口的高度
        var originalHeight=document.documentElement.clientHeight || document.body.clientHeight;
        window.onresize = function (ev) {
            /* console.log(ev)*/
            var  resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
            if(resizeHeight*1<originalHeight*1){ //resizeHeight<originalHeight证明窗口被挤压了

                $('.sure-submit').css('display','none');
                $('.remark-input').addClass('pos_fixed');
                $('.bac_rgba').removeClass('none');

            }else{

                $('.sure-submit').css('display','block');
                $('.remark-input').removeClass('pos_fixed');
                $('.bac_rgba').addClass('none');
            }

        }

        $('.bac_rgba').on('tap',function () {
            $('.remark-input').removeClass('pos_fixed');
            $('.bac_rgba').addClass('none');
            $('.sure-submit').css('display','block');
            document.activeElement.blur();//关闭软键盘
        })


        //订水
        function take_water() {
            //获取订的票数
            var buy_num = $('#mui-numbox-input').val();
            //弹框
            var btnArray = ['取消', '确定'];
            mui.confirm('你确定要购买'+buy_num+'桶水？', '提示', btnArray, function(e) {
                if (e.index == 1) {
                    //获取要传入的参数
                    var id = $('#tickt_id').val();
                    var num = buy_num;
                    var buyer_name = address.name;
                    var mobile = address.mobile;
                    var buyer_address = address.area_info +" "+address.position_address+address.address;
                    var description = $('#remark-input').val();

                    var success = function (res) {
                        console.log(res);
                        //表示是下单的成功页面
                        setTimeout(function() {
                            locationReplace("success.html?type=3");
                        }, 2000);

                    }

                    //调用一键订水购买接口
                    postData(order_add_bill_order,combineParams({
                        id:id,
                        num:num,
                        buyer_name:buyer_name,
                        mobile:mobile,
                        buyer_address:buyer_address,
                        description:description}),success);

                }
            })
        }

    }
    getData(address_list,comParams(),success);

//点击跳转到我的页面
    $('.goods-carts').on('tap',function () {
        redirect('member.html');
    })


//微信支付
    function payment(data) {
        if (window.__wxjs_environment === 'miniprogram') {
            data.show_type = 1;
            var status = false;
            wx.miniProgram.redirectTo({
                url: '/pages/qds/wxpay/index?params=' + escape(JSON.stringify(data))
            })
            var mask = mui.createMask(function () {
                return false;
            });
            mask.show();//显示遮罩
            mui.confirm('', '是否支付成功 ?', ['是', '否'], function (e) {
                if (e.index == 0) {
                    var success = function (res) {
                        if (res.data.result != undefined) {
                            status = res.data.result;
                        }
                        if (status) {
                            /*clearCart();*/
                            locationReplace('success.html?type=4');
                            /*locationReplace('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU1NDc2MzcyNg==#wechat_redirect')*/
                        } else {
                            locationReplace('fail.html?type=3');
                        }
                    };
                    getData(order_check_status, combineParams({id: data.id}), success);
                } else {
                    locationReplace('fail.html?type=3');
                }
            })

        } else {
            wxPay(data, 1);
        }
    }



//是否首次购水
    var fisrt_buy = false;
    setCache('confirm',false);
    initBuyFisrt();
    function initBuyFisrt() {
        var product_num = $('#mui-numbox-input').val();
        var order_success = getCache(mid + "_"+sid+'_'+'order');
        if(order_success == 1){
            /*setCache('confirm',false);*/
            fisrt_buy = false;
            /*//判断是否超出购买范围
            checkFrim();*/

        } else {
            var success = function (res_new) {
                var list = res_new.data.order_list.data;
                var count = 0;
                setCache(mid + "_"+sid+'_'+'order',0);
                /*setCache('confirm',true);*/
                fisrt_buy = true;
                if(!isEmpty(list) && list.length > 0){
                    for(var i=0;i<list.length;i++){
                        if(list[i].status == 20){
                            count++;
                            break
                        }
                    }
                }
                if(count > 0){
                    setCache(mid + "_"+sid+'_'+'order',1);
                    /*setCache('confirm',false);*/
                    fisrt_buy = false;
                }
                /*//判断是否超出购买范围
                checkFrim();*/
            }
            getData(order_index,combineParams({type:1}),success);
        }
    }

//判断当水票购买时 是否水票为0
    function is_rest_num() {
        var status = false;
        var num = $('#current_rest_num').val();
        if(isEmpty(num) || num <= 0){
            status = true;

        }
        if(pay_type == 0 || pay_type == 1){
            status = false;
        }
        if(status){
            ajaxOption.alert = true;
            ajaxOption.confirm_content = '水票为0,点击购买或选其他支付方式';
        }else {
            if(fisrt_buy){
                ajaxOption.confirm = true;
                ajaxOption.confirm_content = confirm_content;
            }
        }
        /*return {
            status: status, // or false
            message: '水票为0,点击购买或选其他支付方式'
        };*/
    }
    checkBuyNum();

    var current_buy_num_content = ''
    var user_buy_max = '';
    var user_buy_min = '';

//判断是否超出购买限量
    function checkBuyNum() {
        var success = function (res) {
            var info = res.data.buy_info;
            if (!isEmpty(info)){
                user_buy_max = info.buy_max;
                user_buy_min = info.buy_min;
            }
        }
        getData(buy_limit,comParams(),success);
    }

    function is_buy_num() {
        var status = true;
        var content = '';
        var product_num =$('#mui-numbox-input').val();
        if(user_buy_max >0 && product_num > user_buy_max){
            //判断购买的商品数量是否超过最大购买数量
            status = false;
            content = content + '最高购买数量 '+ user_buy_max +' 件';
        }
        if(user_buy_min >= 1 && product_num < user_buy_min){
            status = false;
            content = content + '最低购买数量 ' + user_buy_min +' 件';
        }
        //判断范围
        if(pay_type == 2){
            status = true;
        }
        if(!status){
            ajaxOption.alert = true;
            ajaxOption.confirm_content = content;
        }else {
            if(fisrt_buy){
                ajaxOption.confirm = true;
                ajaxOption.confirm_content = confirm_content;
            }
        }
        /*return {
            status: status, // or false
            message: content
        };*/
    }
}



